package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.bodysensor.BodySensorConfigDto;
import com.csun.cmny.provider.model.dto.camera.CameraConfigDto;
import com.csun.cmny.provider.model.dto.csunsleep.SleepConfigAndThresholdDto;
import com.csun.cmny.provider.model.dto.humidistat.HumidistatConfigAndThresholdDto;
import com.csun.cmny.provider.model.vo.csunbody.BodySensorConfigVO;
import com.csun.cmny.provider.model.vo.csuncamera.CameraConfigVO;
import com.csun.cmny.provider.model.vo.csunsleep.SleepConfigAndThresholdVO;
import com.csun.cmny.provider.model.vo.humidistat.HumidistatConfigAndThresholdVO;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/deviceConfig", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcDeviceConfigAndThresholdController extends BaseController {

    @Resource
    private DmcCsunCameraConfigService dmcCsunCameraConfigService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcCsunBodySensorConfigService dmcCsunBodySensorConfigService;
    @Resource
    private DmcCsunHumidistatConfigService dmcCsunHumidistatConfigService;
    @Resource
    private DmcCsunSleepConfigService dmcCsunSleepConfigService;
    @Resource
    private DmcCsunHumidistatLocationService csunHumidistatLocationService;
    @Resource
    private DmcCsunBodySensorLocationService bodySensorLocationService;
    @Resource
    private DmcCsunSleepBindService sleepBindService;
    @Resource
    private DmcCsunCameraLocationService cameraLocationService;
    @Resource
    private DmcCsunGatewayBindService gatewayBindService;
    @Resource
    private DmcSleepCareBindService sleepCareBindService;
    @Resource
    private DmcLifeTransducerDefaultConfigService lifeTransducerDefaultConfigService;

    
    /**
     * 批量摄像头配置
     * @param dto
     * @return com.csun.cmny.provider.controller.life.DmcLifeTransducerConfigController
     */
    @PostMapping(value = "/setCsunCameraConfig")
    public Wrapper setCsunCameraConfig(@RequestBody CameraConfigDto dto) {

        for (Long deviceId : dto.getDeviceIdList()) {
            //校验设备是否绑定了位置
            DmcCsunCameraLocation dmcCsunCameraLocation = cameraLocationService.selectByKey(deviceId);
            if (PublicUtil.isEmpty(dmcCsunCameraLocation)){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,
                        cmnyMessageSource.getMessage("device.notbind.error", null));
            }
        }
        
        int ret = dmcCsunCameraConfigService.setCsunCameraConfig(dto,getLoginAuthDto());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                ret);
    }

    /**
     * 批量人感配置
     * @param dto
     * @return com.csun.cmny.provider.controller.life.DmcLifeTransducerConfigController
     */
    @PostMapping(value = "/setBodySensorConfig")
    public Wrapper setBodySensorConfig(@RequestBody BodySensorConfigDto dto) {

        for (Long deviceId : dto.getDeviceIdList()) {

            //校验设备是否绑定了网关
            DmcCsunGatewayBind dmcCsunGatewayBind = new DmcCsunGatewayBind();
            dmcCsunGatewayBind.setDeviceId(deviceId);
            List<DmcCsunGatewayBind> select = gatewayBindService.select(dmcCsunGatewayBind);
            if (PublicUtil.isEmpty(select)){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,
                        cmnyMessageSource.getMessage("device.notbind.error", null));
            }


            //校验设备是否绑定了位置
            DmcCsunBodySensorLocation dmcCsunBodySensorLocation = bodySensorLocationService.selectByKey(deviceId);

            if (PublicUtil.isEmpty(dmcCsunBodySensorLocation)){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,
                        cmnyMessageSource.getMessage("device.notbind.error", null));
            }
        }

        int ret = dmcCsunBodySensorConfigService.setBodySensorConfig(dto,getLoginAuthDto());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                ret);
    }

    /**
     * 批量温度计配置、阈值
     * @param dto
     * @return com.csun.cmny.provider.controller.life.DmcLifeTransducerConfigController
     */
    @PostMapping(value = "/setCsunHumidistatConfigAndThreshold")
    public Wrapper setCsunHumidistatConfigAndThreshold(@RequestBody HumidistatConfigAndThresholdDto dto) throws Exception {

        for (Long deviceId : dto.getDeviceIdList()) {

            //校验设备是否绑定了网关
            DmcCsunGatewayBind dmcCsunGatewayBind = new DmcCsunGatewayBind();
            dmcCsunGatewayBind.setDeviceId(deviceId);
            List<DmcCsunGatewayBind> select = gatewayBindService.select(dmcCsunGatewayBind);
            if (PublicUtil.isEmpty(select)){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,
                        cmnyMessageSource.getMessage("device.notbind.error", null));
            }

            //校验设备是否绑定了位置
            DmcCsunHumidistatLocation dmcCsunHumidistatLocation = csunHumidistatLocationService.selectByKey(deviceId);

            if (PublicUtil.isEmpty(dmcCsunHumidistatLocation)){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,
                        cmnyMessageSource.getMessage("device.notbind.error", null));
            }
        }





        int ret = dmcCsunHumidistatConfigService.setCsunHumidistatConfigAndThreshold(dto,getLoginAuthDto());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                ret);
    }

    /**
     * 批量床垫配置、阈值
     * @param dto
     * @return com.csun.cmny.provider.controller.life.DmcLifeTransducerConfigController
     */
    @PostMapping(value = "/setCsunSleepConfigAndThreshold")
    public Wrapper setCsunSleepConfigAndThreshold(@RequestBody SleepConfigAndThresholdDto dto) {

        for (Long deviceId : dto.getDeviceIdList()) {
            //校验设备是否绑定了位置
            DmcCsunSleepBind dmcCsunSleepBind = sleepBindService.selectByKey(deviceId);
            DmcSleepCareBind dmcSleepCareBind = sleepCareBindService.selectByKey(deviceId);

            if (PublicUtil.isEmpty(dmcCsunSleepBind)&&PublicUtil.isEmpty(dmcSleepCareBind)){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,
                        cmnyMessageSource.getMessage("device.notbind.error", null));
            }
        }

        int ret = dmcCsunSleepConfigService.setCsunSleepConfigAndThreshold(dto,getLoginAuthDto());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                ret);
    }

    /**
     * 摄像头配置查询
     * @param deviceId
     * @return com.csun.cmny.provider.controller.life.DmcLifeTransducerConfigController
     */
    @PostMapping(value = "/getCsunCameraConfig/{deviceId}")
    public Wrapper setCsunCameraConfig(@PathVariable Long deviceId) {

        CameraConfigVO vo = dmcCsunCameraConfigService.getCsunCameraConfig(deviceId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                vo);
    }

    /**
     * 查询人感配置
     * @param deviceId
     * @return com.csun.cmny.provider.controller.life.DmcLifeTransducerConfigController
     */
    @PostMapping(value = "/getBodySensorConfig/{deviceId}")
    public Wrapper getBodySensorConfig(@PathVariable Long deviceId) {

        BodySensorConfigVO vo = dmcCsunBodySensorConfigService.getBodySensorConfig(deviceId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                vo);
    }

    /**
     * 查询温度计配置、阈值
     * @param deviceId
     * @return com.csun.cmny.provider.controller.life.DmcLifeTransducerConfigController
     */
    @PostMapping(value = "/getCsunHumidistatConfigAndThreshold/{deviceId}")
    public Wrapper getCsunHumidistatConfigAndThreshold(@PathVariable Long deviceId) {

        HumidistatConfigAndThresholdVO vo = dmcCsunHumidistatConfigService.getCsunHumidistatConfigAndThreshold(deviceId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                vo);
    }

    /**
     * 查询床垫配置、阈值
     * @param deviceId
     * @return com.csun.cmny.provider.controller.life.DmcLifeTransducerConfigController
     */
    @PostMapping(value = "/getCsunSleepConfigAndThreshold/{deviceId}")
    public Wrapper getCsunSleepConfigAndThreshold(@PathVariable Long deviceId) {

        SleepConfigAndThresholdVO vo = dmcCsunSleepConfigService.getCsunSleepConfigAndThreshold(deviceId);

        //如果这个床垫没有设定阈值,就下发机构阈值
        if(PublicUtil.isEmpty(vo)){
            DmcLifeTransducerDefaultConfig dmcLifeTransducerDefaultConfig = lifeTransducerDefaultConfigService.selectByKey(getLoginAuthDto().getGroupId());
            vo.setMaxBreathThreshold(dmcLifeTransducerDefaultConfig.getBreathMaxTimes());
            vo.setMinBreathThreshold(dmcLifeTransducerDefaultConfig.getBreathMinTimes());
            vo.setMaxHeartbeatThreshold(dmcLifeTransducerDefaultConfig.getHeartbeatMaximum());
            vo.setMinHeartbeatThreshold(dmcLifeTransducerDefaultConfig.getHeartbeatMinimum());
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                vo);
    }

}
